#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "debug.h"
#include "os_api.h"
#include "dev_api.h"
#include "ps_api.h"

#define _STACK_SIZE		(1024*3)
static void* _task_stack = NULL;

static OSTaskRef _task_ref = NULL;

static OSATimerRef _task_delete_timer_ref = NULL;

void DeleteTaskByTimer(UINT32 id)
{
    OSA_STATUS status;

    if(_task_ref)
    {
        status = OSATaskDelete(_task_ref);
        ASSERT(status == OS_SUCCESS);
        _task_ref = NULL;
	   
        if(_task_stack)
        {
            free(_task_stack);
            _task_stack = NULL;
        }
    }

    OSATimerDelete (_task_delete_timer_ref);
}

void wait_network_ready(void)
{
    int count = 30;
    while(count-- && ps_GetNetworkReady() == FALSE){	
        OSATaskSleep(200);
    }

    if (count <= 0){
        LOG_PRINTF("dev_CtrlReset.\n");
        dev_CtrlReset(0);
    }
}

static void _task(void *ptr)
{
    int index = 0;
	BOOL ret = FALSE;
    char plmn[16] = {0};
    char buffer[512] = {0};
	PsSignalInfo signal_info = {0};
	PsLteScellInfo lte_s_info = {0};
    PsLteNcellsInfo lte_n_info = {0};
    
	wait_network_ready();

    while(1){

        /*
        *	================================
        *	rsrp 和 rsrq 不同显示换算: 适用ps_api.h中获取的所有rsrp和rsrq
        *	0 rsrp < -140 dBm 
        *	1 -140 dBm ≤ rsrp < -139 dBm 
        *	2 -139 dBm ≤ rsrp < -138 dBm 
        *	: : : :
        *	95 -46 dBm ≤ rsrp < -45 dBm 
        *	96 -45 dBm ≤ rsrp < -44 dBm 
        *	97 -44 dBm ≤ rsrp 
        *	255 not known or not detectable
        *	
        *	rsrp = rsrp - 140
        *	================================
        *	
        *	0 rsrq < -19.5 dB 
        *	1 -19.5 dB ≤ rsrq < -19 dB 
        *	2 -19 dB ≤ rsrq < -18.5 dB 
        *	: : : : 
        *	32 -4 dB ≤ rsrq < -3.5 dB 
        *	33 -3.5 dB ≤ rsrq < -3 dB 
        *	34 -3 dB ≤ rsrq 
        *	255 not known or not detectable
        *	
        *	rsrq = 0.5 * rsrq - 19.5
        *	================================
        */
        LOG_PRINTF("ps_GetTac : %d\n", ps_GetTac());
        LOG_PRINTF("ps_GetCellId : %d\n", ps_GetCellId());       
        LOG_PRINTF("ps_GetCsq : %d\n", ps_GetCsq());
        LOG_PRINTF("ps_GetRsrp : %d\n", ps_GetRsrp());
        LOG_PRINTF("ps_GetRsrp dbm : %d\n", (ps_GetRsrp() - 140));

        ret = ps_GetServerCellInfo(&lte_s_info, 2);
        LOG_PRINTF("ps_GetServerCellInfo ret %d\n", ret);
        if (ret == TRUE){
            if (lte_s_info.lenOfMnc == 2){
                LOG_PRINTF("ps_GetServerCellInfo plmn: %03X%02X\n", lte_s_info.mcc, lte_s_info.mnc);
            } else if (lte_s_info.lenOfMnc == 3){
                LOG_PRINTF("ps_GetServerCellInfo plmn: %03X%03X\n", lte_s_info.mcc, lte_s_info.mnc);            
            }

            LOG_PRINTF("ps_GetServerCellInfo tac: %d, cellId: %d\n", lte_s_info.tac, lte_s_info.cellId);  

            LOG_PRINTF("ps_GetServerCellInfo EuArfcn: %d, phyCellId: %d\n", lte_s_info.dlEuArfcn, lte_s_info.phyCellId);  

            LOG_PRINTF("ps_GetServerCellInfo band: %d, dlBandwidth: %d\n", lte_s_info.band, lte_s_info.dlBandwidth);  

            LOG_PRINTF("ps_GetServerCellInfo other: %d, %d, %d, %d, %d, %d, %d\n", 
                                    lte_s_info.isTdd, 
                                    lte_s_info.subframeAssignment,
                                    lte_s_info.specialSubframePatterns,
                                    lte_s_info.rsrp,
                                    lte_s_info.rsrq,
                                    lte_s_info.sinr,
                                    lte_s_info.srxlev); 
        }


        ret = ps_GetNeighborCellInfo(&lte_n_info, 5);
        LOG_PRINTF("ps_GetNeighborCellInfo ret %d\n", ret);
        if (ret == TRUE){
            
            LOG_PRINTF("lte_n_info.numIntraFreq %d\n", lte_n_info.numIntraFreq);
            for (index = 0; index < lte_n_info.numIntraFreq; index++){
                if (lte_n_info.intraFreq[index].mcc > 0 ){
                    memset(plmn, 0x00, sizeof(plmn));
                    memset(buffer, 0x00, sizeof(buffer));
                    if (lte_n_info.intraFreq[index].lenOfMnc == 2){
                        sprintf(plmn, "%03X%02X", lte_n_info.intraFreq[index].mcc, lte_n_info.intraFreq[index].mnc);
                    } else if (lte_n_info.intraFreq[index].lenOfMnc == 3){
                        sprintf(plmn, "%03X%03X", lte_n_info.intraFreq[index].mcc, lte_n_info.intraFreq[index].mnc);        
                    }
                    sprintf(buffer, "intraFreq cell info: %s,%d,%ld,%d,%d,%d,%ld,%d\n", 
                                        plmn,
                                        lte_n_info.intraFreq[index].tac,
                                        lte_n_info.intraFreq[index].cellId,
                                        lte_n_info.intraFreq[index].rsrp,
                                        lte_n_info.intraFreq[index].rsrq,                                    
                                        lte_n_info.intraFreq[index].srxlev,
                                        lte_n_info.intraFreq[index].euArfcn,
                                        lte_n_info.intraFreq[index].phyCellId);

                    LOG_PRINTF(buffer);
                }
            }
            
            LOG_PRINTF("lte_n_info.numInterFreq %d\n", lte_n_info.numInterFreq);
            for (index = 0; index < lte_n_info.numInterFreq; index++){
                if (lte_n_info.interFreq[index].mcc > 0 ){
                    memset(plmn, 0x00, sizeof(plmn));
                    memset(buffer, 0x00, sizeof(buffer));
                    if (lte_n_info.interFreq[index].lenOfMnc == 2){
                        sprintf(plmn, "%03X%02X", lte_n_info.interFreq[index].mcc, lte_n_info.interFreq[index].mnc);
                    } else if (lte_n_info.interFreq[index].lenOfMnc == 3){
                        sprintf(plmn, "%03X%03X", lte_n_info.interFreq[index].mcc, lte_n_info.interFreq[index].mnc);        
                    }
                    sprintf(buffer, "interFreq cell info: %s,%d,%ld,%d,%d,%d,%ld,%d\n", 
                                        plmn,
                                        lte_n_info.interFreq[index].tac,
                                        lte_n_info.interFreq[index].cellId,
                                        lte_n_info.interFreq[index].rsrp,
                                        lte_n_info.interFreq[index].rsrq,                                    
                                        lte_n_info.interFreq[index].srxlev,
                                        lte_n_info.interFreq[index].euArfcn,
                                        lte_n_info.interFreq[index].phyCellId);

                    LOG_PRINTF(buffer);
                }
            }
        }        

        OSATaskSleep(3*200);
    }

	OSATimerCreate(&_task_delete_timer_ref);
	OSATimerStart(_task_delete_timer_ref, 400, 0, DeleteTaskByTimer, 0);
}


int main(void)
{	
    int ret;

	_task_stack = malloc(_STACK_SIZE);
    ASSERT(_task_stack != NULL);
	
    ret = OSATaskCreate(&_task_ref, _task_stack, _STACK_SIZE, 100, "_task", _task, NULL);
    ASSERT(ret == OS_SUCCESS);
	
	return 0;
}